/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package framework.view;

import framework.model.Account;
import framework.model.Customer;
import framework.model.Factory;
import framework.model.FinancialCompany;
import framework.model.IAccount;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author ThanhNguyen
 */
public class AccountTableModel extends AbstractTableModel implements Observer {

    protected String[] columnNames;
    protected FinancialCompany financialCompany;

    public AccountTableModel() {
        columnNames = new String[]{"Name", "Street", "City", "State", "Zip", "Balance"};
        financialCompany = Factory.getInstance().getFinancialCompany();
        financialCompany.addObserver(this);
    }

    @Override
    public int getRowCount() {
        return financialCompany.getAccounts().size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        List<Account> accounts = financialCompany.getAccounts();
        IAccount account = accounts.get(rowIndex);
        Customer customer = account.getCustomer();
        switch (columnIndex) {
            case 0:
                return customer.getName();
            case 1:
                return customer.getAddress().street;
            case 2:
                return customer.getAddress().city;
            case 3:
                return customer.getAddress().state;
            case 4:
                return customer.getAddress().zip;
            case 5:
                return account.getCurrentBalance();
            default:
                return null;
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        fireTableDataChanged();
    }
}